<?php
/**
 * Description of user_model
 *
 * @author aquilax
 */
class User_model extends Model{

/*TODO: Implement me*/

  private $userrow = FALSE;

  function _getDefault($array, $name, $default = FALSE){
    if (is_array($array) &&isset($array[$name])){
      return $array[$name];
    }
    return $default;
  }

  function load(){
    $this->userrow = $this->session->userdata('userrow');
    return $this->userrow != FALSE;
  }

  function login(){
    $username = $this->input->post('username');
    $password = $this->input->post('password');
    $select = array(
      'u.id AS user_id',
      'u.username',
      'u.email',
      'UNIX_TIMESTAMP(u.created) AS created',
      'uf.last_login',
      'uf.group_id',
      'UNIX_TIMESTAMP(uf.created) AS joined',
    );
    $this->db->select($select);
    $this->db->where('u.username', $username);
    $this->db->where('u.password', $password);
    $this->db->where('uf.forum_id', $this->forum_id);
    $this->db->join('user_forum uf', 'u.id = uf.user_id', 'LEFT');
    $query = $this->db->get('user u', 1);
    $row = $query->row_array();
    if($row){
      $this->session->set_userdata(array('userrow' => $row));
      return TRUE;
    } else {
      return FALSE;
    }
  }

  function logout(){
    $array_items = array('userrow' => '');
    $this->session->unset_userdata($array_items);
  }

  /*TODO: Implement me*/
  function updateLastLogin($user_id){

  }

  function user_id(){
    return $this->_getDefault($this->userrow, 'user_id');
  }

  function logged(){
    return $this->_getDefault($this->userrow, 'user_id') != FALSE;
  }
}
?>
